$(function () {
	//回显
	echoCostSharing();
	
    //是否验证成功
    $('#shortDriverForm, #longDriverForm').validationEngine('attach', {
        validationEventTrigger : 'blur',	//当提交的时候验证是否合法
        promptPosition: 'inline',
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
    		if (valid){
    			var platformCostSharing = Number($('#' + form.context.id + ' input[id=platformCostSharing]').val());
    			var companyCostSharing = Number($('#' + form.context.id + ' input[id=companyCostSharing]').val());
    			var driverCostSharing = Number($('#' + form.context.id + ' input[id=driverCostSharing]').val());
    			var sum = platformCostSharing + companyCostSharing + driverCostSharing;
    			if(sum <= 100){
    				updateCostSharingInfo(form.context.id);
    			}else{
    				layer.msg('输入值的总和不能大于100', {icon: 2});
    			}   
    			 
            }
        }
    });
    
});

//点击编辑回显数据
function echoCostSharing(){
    $.ajax({
        url: "../costsharing/getCostSharingConfigInfo.htm",
        cache: false,
        async : false,
        type: "get",
        dataType: "json",
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                for(var i=0; i < data.length; i++){
                	if(data[i].driverType == 1){
                		$("#shortDriverForm").formatForm(data[i]);
                	}else if(data[i].driverType == 2){
                		$("#longDriverForm").formatForm(data[i]);
                	}
                }
                return;
            }
        }
    });
}

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//修改分成
function updateCostSharingInfo(fromContext) {
    $.ajax({
        url: "../costsharing/modifyCostSharingConfigInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#" + fromContext).serialize(),
        success: function (result) {
            if (result.status == 0) {
            	layer.msg(result.message, {icon: 1});
            	setTimeout(function(){$(window.parent.document).find(".page-tabs-content a.active").find("i").trigger("click");},00);
            } else {
                layer.msg(result.message, {icon: 2});
            }
        }
    });
}
